package com.doit.day01

import java.util
import scala.collection.immutable

object _06_for循环 {
  def main(args: Array[String]): Unit = {

    val arr: Array[Int] = Array(1, 2, 3, 4, 5)

    for (elem <- arr) {
      println(elem)
    }

    println("==================帅气得分割线============================")

    //那我想打印1 - 10 不就是一个序列  seq()
    val seq: Seq[Int] = 1 to 10//1-10 前闭后闭
//    val inclusive:Seq[Int] = 1.to(10)//语法糖
    val seq1: Seq[Int] = 1 until 10//1-10 前闭后开
//    val range: Range = 1.until(10)
    for (elem <- seq) {
      println(elem)
    }

    for(elem <- 1 to 10){
      println(elem)
    }



    println("=====================骚气分分割线=================================")
    val inclusive: Range.Inclusive = 1.to(10, 3)

    for (elem <- inclusive) {
      println(elem)
    }


    //循环步长  by
    for (elem <- 1 to 10 by 3 reverse) {
      println(elem)
    }

    for(elem <- 1 to 10 reverse){
      println(elem)
    }

    for(elem <- 10 to 1 by -3){
      println(elem)
    }

    println("==================无法形容了=============================")

    //需求：我想要打印1-10 但是我就是不想要5
    for (elem <- 1 to 10){
      if(elem != 5){
        println(elem)
      }
    }


    //循环得守卫
    for(elem <- 1 to 10 if elem != 5){
      println(elem)
    }

    //打印所有得水仙花数  三位数  各位得三次方  + 十位得三次方 + 百位得三次方 = 它本身

    //双重for循环
    for(i <- 1 to 9){
      for(j <- 1 to i){
        print(s"$i * $j = ${i*j}\t")
      }
      println()
    }

    for(i <- 1 to 9;j <- 1 to i){
      print(s"$i * $j = ${i*j}\t")
      if(i == j) println()
    }


    println("===========循环得引入变量=============")

    for(i <- 1 to 3; j = 4 - i) {
      println("i=" + i + " j=" + j)
    }

    var j = 0
    for(i <- 1 to 3){
      j = 4 - i
      println("i=" + i + " j=" + j)
    }


    println("==============循环得返回值====================")

    val list: util.ArrayList[Int] = new util.ArrayList[Int]()

    //想把所有得偶数全部收集起来
    for (i <- 1 to 10) {
      if (i % 2 == 0) {
        list.add(i)
      }
    }
    println(list)

    //yield 关键字 ==》 用来收集函数体中得返回值
    val res: immutable.Seq[Int] = for (i <- 1 to 10) yield{
      if(i %2 == 0){
        i
      }else{
        -1
      }
    }
    //用来过滤得
//    val ints = res.filter(_ > 0)

    println(res)



  }
}
